package com.wannengbang.cloudleader.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.bean.ShopOrderListBean;
import com.wannengbang.cloudleader.mine.adapter.OrderProductListAdapter;
import com.wannengbang.cloudleader.utils.DateTimeUtil;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 订单列表
 */
public class ShopOrderListAdapter extends RecyclerView.Adapter<ShopOrderListAdapter.ViewHolder> {


    private List<ShopOrderListBean.DataBean.ItemListBean> listsBeans;
    private Context context;

    public ShopOrderListAdapter(List<ShopOrderListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    private OnCallBackListener onCallBackListener;

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_shop_my_order_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //状态:1-已下单,2-已支付,3-已发货,4-已签收,5 申请取消中 6-已取消
        ShopOrderListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        holder.tvBtnSign.setVisibility(View.GONE);
        holder.tvBtnPay.setVisibility(View.GONE);
        if (dataBean.getStatus() == 1) {
            holder.tvOrderStatus.setText("待付款");
            holder.tvBtnPay.setVisibility(View.VISIBLE);
        } else if (dataBean.getStatus() == 2) {
            holder.tvOrderStatus.setText("待发货");
        } else if (dataBean.getStatus() == 3) {
            holder.tvOrderStatus.setText("待收货");
            holder.tvBtnSign.setVisibility(View.VISIBLE);
        } else if (dataBean.getStatus() == 4) {
            holder.tvOrderStatus.setText("已完成");
        } else if (dataBean.getStatus() == 5) {
            holder.tvOrderStatus.setText("申请取消中");
        } else if (dataBean.getStatus() == 6) {
            holder.tvOrderStatus.setText("已取消");
        }

        holder.tvTotalPrice.setText(dataBean.getIntegral_price());
        String freight = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getShop_order_goods().get(0).getFreight()) / 100);

        holder.tvFreightPrice.setText(freight + "元");

        holder.tvOrderNo.setText("订单编号：" + dataBean.getId());
        holder.tvCreateTime.setText("下单时间：" + DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getCreate_time()) * 1000)));

        ShopOrderProductListAdapter listAdapter = new ShopOrderProductListAdapter(dataBean.getShop_order_goods() == null ? new ArrayList<>() : dataBean.getShop_order_goods());
        holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
        holder.recyclerView.setAdapter(listAdapter);

        holder.tvBtnPay.setOnClickListener(v -> {
            onCallBackListener.onCallBack(1, position);
        });
        holder.tvBtnSign.setOnClickListener(v -> {
            onCallBackListener.onCallBack(2, position);
        });
        holder.recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return holder.itemView.onTouchEvent(event);
            }
        });
        holder.itemView.setOnClickListener(v -> {
            onCallBackListener.onCallBack(3, position);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_order_status)
        TextView tvOrderStatus;
        @BindView(R.id.tv_freight_price)
        TextView tvFreightPrice;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.tv_create_time)
        TextView tvCreateTime;
        @BindView(R.id.tv_order_no)
        TextView tvOrderNo;
        @BindView(R.id.tv_total_price)
        TextView tvTotalPrice;
        @BindView(R.id.tv_btn_sign)
        TextView tvBtnSign;
        @BindView(R.id.tv_btn_pay)
        TextView tvBtnPay;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnCallBackListener {
        void onCallBack(int type, int position);
    }
}
